// JavaScript Document

window.onload = function () {
    var operationArae = document.getElementsByClassName('ident');
    operationArae[0].style.display = "block";
    var clickArea = document.getElementsByClassName('sidesmenu');
    for (var i = 0; i < clickArea.length - 1; i++) {
        ~(function (lockindex) {
            clickArea[i].onclick = function () {
                clearAlldisplay();
                if (lockindex >= 0) {
                    operationArae[lockindex].style.display = "block";
                    switch (lockindex) {
                        case 1:
                            requestData("http://localhost:8080/allRoominfo.action", function (data) {
                                var contentarea = "";
                                data = JSON.parse(data);
                                for (var i = 0; i < data.length; i++) {
                                    var status = "";
                                    var operation = "";
                                    if(!data[i].roomStatus)
                                    {
                                        status = "空";
                                        operation = " <td> <a  class='btn btn-primary' data-target='#myModal' data-toggle='modal' onclick='show("+data[i].roomId+")'>入住</a> </td>";
                                    }else{
                                        status = "满";
                                        operation = " <td> <a class='btn btn-danger' data-toggle='modal' data-target='#checkOut'  onclick='checkOutshow("+data[i].roomId+")'>退房</a> </td>";

                                    }

                                    var starthtml = "<tr>";
                                    var star = "<td>"+data[i].roomId+"</td>" +
                                                "<td>"+data[i].roomLevel+"</td>" +
                                                "<td>"+data[i].roomType+"</td>" +
                                                "<td>"+data[i].roomPrice+"</td>" +
                                                "<td>"+status+"</td>" +
                                                operation;
                                    var endhtml = "</tr>";
                                    contentarea += starthtml + star + endhtml;
                                }
                                document.getElementById("user").innerHTML = contentarea;
                            });
                            break;
                        case 2:
                            requestData("http://localhost:8080/selectInroom.action",function (data) {
                                var contentArea = "";
                                data = JSON.parse(data);
                                for (var object in data[0])
                                {
                                    var argu = "'"+data[0][object].roomId+"','"+data[0][object].hotelClientName+"','"+data[0][object].hotelClientIdcard+"','"
                                        +data[0][object].hotelClientTel+"','"+data[0][object].hotelInTime+"'";
                                    var starthtml = "<tr>";
                                    var star = "<td>"+data[0][object].roomId+"</td>" +
                                        "<td>"+data[0][object].hotelClientName+"</td>" +
                                        "<td>"+data[0][object].hotelClientIdcard+"</td>" +
                                        "<td>"+data[0][object].hotelClientTel+"</td>" +
                                        "<td>"+data[0][object].hotelInTime+"</td>" +
                                        "<td><a class='btn btn-primary' data-toggle='modal' data-target='#updateIn' onclick=\"updateInfo("+argu+")\">修改信息</a></td>";
                                    var endhtml = "</tr>";
                                    console.log("updateInfo("+argu+")");
                                    contentArea += starthtml + star + endhtml;
                                }
                                console.log(contentArea);
                                document.getElementById("allInfo").innerHTML = contentArea;
                            });
                            break;
                        default:
                            break;
                    }
                }
            }
        })(i);
    }

    function clearAlldisplay() {
        for (var singleArea of operationArae) {
            singleArea.style.display = "none";
        }
    }

    function requestData(url, recall) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                recall(xmlhttp.responseText);
            }
        };
        xmlhttp.open('GET', url, true);
        xmlhttp.send();
    }

    function requestDataPost(url, data, recall) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                recall(xmlhttp.responseText);
            }
        }
        xmlhttp.open("POST", "updateuser.action", true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
        xmlhttp.send(data);
    }

    function listenner() {
        var Get = document.getElementsByClassName("get");
        var inputValues = new Array();
        for (var i = 0; i < Get.length; i++) {
            ~(function (lockindex) {
                Get[lockindex].addEventListener("click", function (e) {
                    var inputPart = e.target.parentNode.parentNode.getElementsByTagName("input");
                    var selectPart = e.target.parentNode.parentNode.getElementsByTagName("select")
                    for (var inputIndex = 0; inputIndex < inputPart.length - 1; inputIndex++) {
                        ~(function (inputLockIndex) {
                            inputValues[inputLockIndex] = inputPart[inputLockIndex].value;
                            if (inputLockIndex === inputPart.length - 2) {
                                inputValues[3] = selectPart[0].options[selectPart[0].selectedIndex].value;
                                var sendInfo = "newone.id=" + inputValues[0] + "&newone.name=" + inputValues[1] + "&newone.password=" + inputValues[2] + "&newone.role=" + inputValues[3];
                                requestDataPost("http://localhost:8080/SSHDemo/updateuser.action", encodeURI(sendInfo), function (data) {
                                    if (data === "GOOD") {
                                        alert("更新人员成功");
                                        requestData("http://localhost:8080/SSHDemo/alluser.action", function (data) {
                                            buildUpdateBody(data);
                                            listenner();
                                        });
                                    }
                                });
                            }
                        })(inputIndex);
                    }
                });
            })(i);
        }
    }

    function buildUpdateBody(data) {
        console.log("this is a update info click");
        var contentarea = "";
        data = JSON.parse(data);
        for (var i = 0; i < data.length; i++) {
            var starthtml = "<tr>";
            var star = template(data[i]);
            var endhtml = "</tr>";
            contentarea += starthtml + star + endhtml;
        }
        document.getElementById("updateuser").innerHTML = contentarea;
    }

    function template(data) {

        var template;
        if (data.role == 1) {
            template = "<td><input style='width:100px;' value='" + data.id + "' type='text' readonly='true' name='newone.id' /></td>" +
                "<td><input style='width:100px;' value='" + data.name + "' type='text' name='newone.name'/></td>" +
                "<td><input style='width:100px;' value='" + data.password + "' type='text' name='newone.password' /></td>" +
                "<td><select name='newone.role'><option value='false'>普通成员</option><option selected value='true'>管理员</option></select></td>" +
                "<td><input class='get btn btn-primary' type='button' value='更新' /></td>";
        } else if (data.role == 0) {
            template = "<td><input style='width:100px;' value='" + data.id + "' type='text' readonly='true' name='newone.id' /></td>" +
                "<td><input style='width:100px;' value='" + data.name + "' type='text' name='newone.name'/></td>" +
                "<td><input style='width:100px;' value='" + data.password + "' type='text' name='newone.password' /></td>" +
                "<td><select name='newone.role'><option selected value='false'>普通成员</option><option  value='true'>管理员</option></select></td>" +
                "<td><input class='get btn btn-primary' type='button' value='更新' /></td>";
        }
        return template;
    }
};